//
//  SubExpressVC.m
//  LazyCat
//
//  Created by linhui on 16/1/6.
//  Copyright (c) 2016年 zhanshu. All rights reserved.
//

#import "SubExpressVC.h"
#import "expressTableViewCell.h"
#import "AddExpressVC.h"
#import "EditExpressVC.h"
#import "MJChiBaoZiHeader.h"
#import "MJChiBaoZiFooter.h"
@interface SubExpressVC ()
{
    NSMutableArray *_dataArray;
    NSMutableArray *_editArr;
    NSIndexPath *_currentIndexPath;
    NSString * _urlIndex;//起始页
    BOOL _isLoading;
    NSString *_totalCount;//总页数
    UIView *_backTView;//没订单时背部提醒
}
@end

@implementation SubExpressVC


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BackColor;
    UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)];
    lineV.alpha = 0.2;
    lineV.backgroundColor = [UIColor lightGrayColor];
    [self createTableV];//创建表视图
    [self loadData];
    [self addRefresh];
}
-(void)createTableV {//创建表视图
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-114) style:UITableViewStyleGrouped];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[expressTableViewCell class] forCellReuseIdentifier:@"Cell"];
    _backTView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64)];
    _backTView.backgroundColor = BackColor;
    _backTView.hidden = YES;
    [self.view addSubview:_backTView];
    UIImageView *backImage = [[UIImageView alloc]initWithFrame:CGRectMake((ScreenWidth - 80)/2, 100, 80, 90)];
    backImage.image = [UIImage imageNamed:@"noexpress"];
    [_backTView addSubview:backImage];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((ScreenWidth - 200)/2, backImage.frame.origin.y + 90, 200, 50)];
    label.textColor = SetColor(153, 153, 153, 1);
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"您暂时还没有快递信息";
    [_backTView addSubview:label];
    _urlIndex = @"1";
    _dataArray = [[NSMutableArray alloc]init];
    _isLoading = NO;
    MJRefreshAutoFooter *foot = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    self.tableView.footer = foot;
}
- (void)loadData {
    NSString *kUrl;
    if(self.currentPage == 0){
        kUrl = [NSString stringWithFormat:expressServiceUrl,@"15",_urlIndex,[AppData share].username,@"0"];
    }else if(self.currentPage == 1){
        kUrl = [NSString stringWithFormat:expressServiceUrl,@"15",_urlIndex,[AppData share].username,@"1"];
    }else if(self.currentPage == 2){
        kUrl = [NSString stringWithFormat:expressServiceUrl,@"15",_urlIndex,[AppData share].username,@"2"];
    }
    if ([_urlIndex isEqualToString:@"1"]) {
        [_dataArray removeAllObjects];
    }
    [NTNetUtil sendRequestWithUrl:kUrl requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
        NSDictionary *dic = networkRequest.responseData;
        NSLog(@"%@",networkRequest.responseData);
        _totalCount = [dic objectForKey:@"totalpage"];
        NSArray *arr = [dic objectForKey:@"expresses"];
        [_dataArray addObjectsFromArray:arr];
        _isLoading = NO;
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        //        [_loading dismiss];
        //
        //        [_loading removeFromSuperview];
        if ([_totalCount isEqualToString:@"0"] || _totalCount == nil) {
            _backTView.hidden = NO;
        }else {
            _backTView.hidden = YES;
        }
        [self.tableView reloadData];
    }];
    if (kNetworkNotReachability) {
        //        [_loading dismiss];
        //        [_loading removeFromSuperview];
    }
}
- (void)addRefresh {
    if (_isLoading) {
        return;
    }
    _isLoading = YES;
    MJChiBaoZiHeader *header = [MJChiBaoZiHeader headerWithRefreshingBlock:^{
        _urlIndex = @"1";
        [header beginRefreshing];
        [self loadData];
    }];
    self.tableView.header = header;
    header.lastUpdatedTimeLabel.hidden = YES;
}
- (void)loadMore {
    if (_isLoading) {
        return;
    }
    _isLoading = YES;
    if ([_urlIndex integerValue] >= [_totalCount integerValue]) {
        [[MessShowView share]showWithMess:@"没有更多数据"];
        return ;
    }else{
        _urlIndex = [NSString stringWithFormat:@"%ld",[_urlIndex integerValue]+1];
        [self loadData];
    }
}

//增加
- (void)addExpress:(NSMutableArray *)addArr {
//    [_dataArray addObject:addArr];
}
//编辑
- (void)sendMessage:(NSMutableArray *)messageArr {
//    _editArr = messageArr;
//    [_dataArray insertObject:messageArr atIndex:_currentIndexPath.row];
//    [_dataArray removeObjectAtIndex:_currentIndexPath.row+1];
//    NSLog(@"%@",_dataArray);
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    expressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.label1.text = [NSString stringWithFormat:@"快递单号:%@(%@)",[_dataArray[indexPath.row]objectForKey:@"expressbillid"],[_dataArray[indexPath.row]objectForKey:@"couriertypestr"]];
    cell.label2.text = [NSString stringWithFormat:@"手机号码:%@",[_dataArray[indexPath.row]objectForKey:@"mobile"]];
    cell.label3.text = [NSString stringWithFormat:@"货柜号:%@",[_dataArray[indexPath.row]objectForKey:@"expressid"]];
//    cell.label4.text = [NSString stringWithFormat:@"%@",[_dataArray[indexPath.row]objectForKey:@"expressbillid"]];
    cell.label5.text = [NSString stringWithFormat:@"备注:%@",[_dataArray[indexPath.row]objectForKey:@"receivetypestr"]];
    return cell;
//    "couriertypestr": "韵达快递",
//    "couriertype": "2",
//    "expressbillid": "1236544754",
//    "expressid": "76",
//    "insteadsendcost": "0.00",
//    "isinsteadsend": "0",
//    "isreceived": "0",
//    "mobile": "13390708478",
//    "receivetime": "",
//    "receivetypestr": "",
//    "receivetype": "",
//    "storageracksid": ""
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    EditExpressVC *editVC = [[EditExpressVC alloc]init];
    editVC.orderNum = [_dataArray[indexPath.row]objectForKey:@"expressbillid"];
    editVC.EXcompany = [_dataArray[indexPath.row]objectForKey:@"couriertypestr"];
    //editVC.EXmoney;
    editVC.EXnum = [_dataArray[indexPath.row]objectForKey:@"expressid"];
    editVC.EXphoneMum = [_dataArray[indexPath.row]objectForKey:@"mobile"];
    editVC.EXbeizhu = [_dataArray[indexPath.row]objectForKey:@"receivetypestr"];
    editVC.isDaiSong = YES;
    editVC.isRecived = YES;
    if (_myBlock) {
        _myBlock(editVC);
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.00001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 105;
}
@end

